package com.example.lsy.zyfypt_227lsy.model;

import com.example.lsy.zyfypt_227lsy.bean.Bean;
import com.example.lsy.zyfypt_227lsy.iface.Iface;
import com.example.lsy.zyfypt_227lsy.iface.Listener;
import com.example.lsy.zyfypt_227lsy.service.Service;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class Model implements Iface{

    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造方法
    public Model(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取文章数据
    @Override
    public void getResultList(String mod, int page, String sessionID, final Listener listener) {
        Service service=retrofit.create(Service.class);
        Call<List<Bean>> call=service.getResult(mod, page, sessionID);
        call.enqueue(new Callback<List<Bean>>() {
            @Override
            public void onResponse(Call<List<Bean>> call, Response<List<Bean>> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    listener.onResponse(response.body());
                }else{
                    listener.onFail("on response fail");
                }
            }

            @Override
            public void onFailure(Call<List<Bean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getResultListNew(String mod, int page, String sessionID, final Listener listener) {
        Service service=retrofit.create(Service.class);
        Call<List<Bean>> call=service.getList(mod, page, sessionID);
        call.enqueue(new Callback<List<Bean>>() {
            @Override
            public void onResponse(Call<List<Bean>> call, Response<List<Bean>> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    listener.onResponse(response.body());
                }else{
                    listener.onFail("on response fail");
                }
            }

            @Override
            public void onFailure(Call<List<Bean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
